package com.jyb.qiyefuwu.view.fragment.usercenter;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.FragmentNeedFollowChildBinding;
import com.jyb.qiyefuwu.databinding.FragmentNeedFollowListBinding;
import com.jyb.qiyefuwu.entity.NeedFollowListBean;
import com.jyb.qiyefuwu.router.PathHome;
import com.jyb.qiyefuwu.view.adpater.NeedFollowListAdapter;
import com.jyb.qiyefuwu.viewmodel.NeedFollowModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 * 作者:陈方凯
 * 日期:2020/09/17 0024
 * 说明:关注-需求大厅列表
 */
public class NeedFollowListFragment extends AppLazyFragment {

    NeedFollowModel mModel;
    private NeedFollowListAdapter mAdapter;

    private FragmentNeedFollowListBinding mBinding;
    private int type;
    private boolean isLoadMore;

    public static NeedFollowListFragment newInstance(int position) {
        NeedFollowListFragment fragment = new NeedFollowListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("data", position);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_need_follow_list, container, false);
        return mBinding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        mModel = new ViewModelProvider(this).get(NeedFollowModel.class);
        mModel.attentionNo.setValue(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        type = getArguments().getInt("data", 0);
        mModel.getData(type, false);
        initAdapter();
        observe();
    }

    private void initAdapter() {
        mAdapter = new NeedFollowListAdapter(getActivity(), null);
        mBinding.recyclerView.setAdapter(mAdapter);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                mModel.getData(type, false);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                mModel.getData(type, true);
            }
        });
        mAdapter.setOnClickFollow(new NeedFollowListAdapter.IFollow() {
            @Override
            public void cancelFollow(NeedFollowListBean item, int position) {
                mModel.saveAttention(type, item.getId() + "", item.getDataId());
            }
        });
        mAdapter.setOnItemClick(new AdapterOnItemClick<NeedFollowListBean>() {
            @Override
            public void onItemClick(NeedFollowListBean needFollowListBean, int position) {
//                ActivityJump.startActivity(AppRouteConstant.QiYeFuWu.ROUTER_PATH_NEWNEEDDETAILSACTIVITY, Pair.create("data", needFollowListBean.getId()+""));
            }
        });
    }

    private void observe() {
        mModel.followList.observe(this, needFollowListBeans -> {
            followList(needFollowListBeans, isLoadMore);
        });
        mModel.followStatus.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(getActivity(),"已取消关注");
                mModel.getData(type, false);
            }
        });
    }

    /**
     * 需求关注列表
     *
     * @param list
     * @param isLoadMore
     */
    public void followList(List<NeedFollowListBean> list, boolean isLoadMore) {
        mBinding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.loadMoreComplete();
                mAdapter.addDatas(list);
            } else {
                mBinding.recyclerView.setNoMore(true);
            }
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
                mBinding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                mAdapter.setDatas(list);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}